/**
 * 如何中断某个单一任务
 * Executor展示了基本的interrupt()用法
 *
 * @author fireway
 * @since 2019年 01月 27日 星期日 06:57:48 CST
 */
package concurrency;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

class SleepBlocked implements Runnable {
    @Override
    public void run() {
        System.out.println("enter SleepBlocked.run()");
        try {
            TimeUnit.SECONDS.sleep(100);
        } catch (InterruptedException e) {
            System.out.println("SleepBlocked.run() occur InterruptedException");
        }
        System.out.println("exit SleepBlocked.run()");
    }
}

class IOBlocked implements Runnable {
    private InputStream mIn;

    public IOBlocked(InputStream is) {
        mIn = is;
    }

    @Override
    public void run() {
        System.out.println("enter IOBlocked.run()");
        try {
            mIn.read();
        } catch (IOException e) {
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Interrupted from blocked I/O");
            } else {
                throw new RuntimeException(e);
            }
        }
        System.out.println("exit IOBlocked.run()");
    }
}

class SynchronizedBlocked implements Runnable {
    public SynchronizedBlocked() {
        new Thread() {
            public void run() {
                f();  // Lock acquired by this thread
            }
        } .start();
    }

    @Override
    public void run() {
        System.out.println("enter SynchronizedBlocked.run()");
        f();
        System.out.println("exit SynchronizedBlocked.run()");
    }

    public synchronized void f() {
        // Never releases lock
        while (true) {
            Thread.yield();
        }
    }
}

public class Interrupting {
    private static ExecutorService sExec = Executors.newCachedThreadPool();

    private static void test(Runnable r) throws InterruptedException {
        Future<?> f = sExec.submit(r);
        TimeUnit.MILLISECONDS.sleep(100);
        System.out.println("interrupt begin: " + r.getClass().getName());
        f.cancel(true);
        System.out.println("interrupt end: " + r.getClass().getName());
    }

    public static void main(String[] args) {
        try {
            test(new SleepBlocked());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            test(new IOBlocked(System.in));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            test(new SynchronizedBlocked());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("abort with System.exit(0)");
        System.exit(0);
    }
}
